<?php
namespace app\adminapiv3\service;

/**
 * @name: 主页逻辑
 * @Param: 
 * @Description: 
 * @Author: foo
 * @Date: 2019-10-22 17:39:24
 */
use app\common\service;
class Index extends Base 
{
    /**
     * @name 主机资源使用率
     * @author wx
     * @date 2020/6/1 9:22
     */
    function hostResource()
    {
        $pcPath = './config/server_pcinfo.json';
        if (!file_exists($pcPath)) {
            jsonErrorOut('系统文件丢失,请联系技术人员',101);
        }
        $content = file_get_contents($pcPath);
        $return = json_decode($content, true);

        return $return;
    }

    /**
     * @name 网络攻击占比
     * @author wx
     * @date 2020/6/1 9:35
     */
    function attackRatio()
    {

        $host_id = $this->findGroupHost();

        //网络攻击统计
        $return['network_attack'] = db('network_attack')->alias('na')
            ->join('ips_type it', 'na.type = it.id')
            ->whereIn('host_id', $host_id)
            ->field('it.show_name as name, count(*) as value')
            ->group('na.type')
            ->select();

        //文件攻击
        $fileAttackServer = new service\FileAttack();
        $return['file_attack'][] = [
            'name' => '文件查杀',
            'value' => $fileAttackServer->ratio(2, $host_id),
        ];
        $FileIllegalModify = new service\FileIllegalModify();
        $return['file_attack'][] = [
            'name' => '文件篡改',
            'value' => $FileIllegalModify->ratio($host_id),
        ];

        $FileEmpowerment = new service\FileEmpowerment();
        $return['file_attack'][] = [
            'name' => '系统提权',
            'value' => $FileEmpowerment->ratio($host_id),
        ];

        $return['file_attack'][] = [
            'name' => '主动防御',
            'value' => $fileAttackServer->ratio(1, $host_id),
        ];


        return $return;
    }


    /**
     * @name: 数据统计
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-22 17:55:50
     */
    function dataStatistics ()
    {
        $HostTagRelation = new service\HostTagRelation();

        $return['assets'] = $this->toDo([$HostTagRelation, 'assets']);//标签比率
        $Host = new service\Host();
        $return['oparent'] = $this->toDo([$Host, 'operant']);//操作系统比率
        $HostView = new service\HostView();
        $return['top']['port'] = $this->toDo([$HostView, 'topPort']);//曲线 端口访问量

        return $return;
    }

    /** 首页排行
     * @name
     * @param $token
     * @return mixed
     * @author wx
     * @date 2020/10/27 17:20
     */
    public function rand($token)
    {
        $userInfo = db('server_user')->where('token', $token)->find();

        if(!$userInfo){
            $this->setError($this->CODE_FAIL, '未知用户');
            return false;
        }



        //查询出所有下级包括自身id
        $groupIdList = db('server_user_group_relation')->alias('sr')
            ->join('server_user_group sg', 'sg.id = sr.group_id')
            ->where('f_group_id', $userInfo['user_group_id'])->column('group_id');

        $hostList = db('Host')->whereIn('group_id', $groupIdList)->column('id');

        $limit = 10;
        //来源IP排行
        $srcIp = db('network_attack')
            ->whereIn('host_id', $hostList)
            ->group('src_ip')
            ->order('attack_count desc')
            ->limit($limit)
            ->column('count(*) as attack_count, src_ip, host_id','src_ip');


        foreach ($srcIp as $k => $v){

            $where['host_id'] = $v['host_id'];
            $where['src_ip'] = $v['src_ip'];
            $addressInfo = db('network_attack_detail')
                ->field('src_country, src_province, src_city, src_detail')
                ->where($where)->find();


            if($addressInfo){
                $srcIp[$k]['address'] = $addressInfo['src_country'].$addressInfo['src_province'].$addressInfo['src_city'].$addressInfo['src_detail'];
            }else{
                $srcIp[$k]['address'] = '';
            }

        }




        //目标IP排行
        $destIp = db('network_attack')
            ->whereIn('host_id', $hostList)
            ->group('dest_ip')
            ->order('attack_count desc')
            ->limit($limit)
            ->column('count(*) as attack_count, dest_ip, host_id','dest_ip');

        foreach ($destIp as $key => $value){

            $where['host_id'] = $value['host_id'];
            $where['dest_ip'] = $value['dest_ip'];
            $addressInfo = db('network_attack_detail')
                ->field('dest_country, dest_province, dest_city, dest_detail')
                ->where($where)->find();


            if($addressInfo){
                $destIp[$key]['address'] = $addressInfo['dest_country'].$addressInfo['dest_province'].$addressInfo['dest_city'].$addressInfo['dest_detail'];
            }else{
                $destIp[$key]['address'] = '';
            }

        }

        //攻击占比
        $type = db('network_attack')->alias('na')
            ->join('ips_type it','na.type = it.id')
            ->whereIn('host_id', $hostList)
            ->where('it.type', 'network')
            ->group('type')
            ->field('na.type, it.show_name, count(*) as attack_count')
            ->select();


        //15天趋势
        $endTime = date('Y-m-d');
        $startTime =  date('Y-m-d',strtotime("$endTime - 15 day"));

        for ($i = 1; $i <= 15; $i++){


           $startDate = date('Y-m-d',strtotime("$startTime + $i day"));
           $endDate = date('Y-m-d',strtotime("$startDate + 1 day"));


           $timeArray = [strtotime($startDate), strtotime($endDate)];

           $trend[$startDate] = db('network_attack')
               ->whereIn('host_id', $hostList)
               ->whereBetween('atime', $timeArray)
               ->count();
//            $tmp['host_id'] = 2;
//            $tmp['atime'] = strtotime($startDate) + 123;
//            $tmp['type'] = rand(1,8);
//            $tmp['level'] = rand(1,4);
//            $tmp['handle'] = rand(0,1);
//            $tmp['src_ip'] = '192.168.25.'.rand(1,10);
//            $tmp['dest_ip'] = '192.168.25.'.rand(1,10);
//
//            $tmpDetail['host_id'] = 2;
//            $tmpDetail['type'] = $tmp['type'];
//            $tmpDetail['src_ip'] = $tmp['src_ip'];
//            $tmpDetail['dest_ip'] = $tmp['dest_ip'];
//
//            $tmpDetail['src_country'] = '中国';
//            $tmpDetail['src_province'] = '重庆';
//            $tmpDetail['src_city'] = '渝北';
//            $tmpDetail['src_detail'] = rand(1,10).'号';
//
//            $tmpDetail['dest_country'] = '中国';
//            $tmpDetail['dest_province'] = '重庆';
//            $tmpDetail['dest_city'] = '渝北';
//            $tmpDetail['dest_detail'] = rand(1,10).'号';
//
//            $tmpArr[] = $tmp;
//            $tmpDetailArr[] = $tmpDetail;

        }

//        db('network_attack')->insertAll($tmpArr);
//        db('network_attack_detail')->insertAll($tmpDetailArr);




        $data['src_ip'] = $srcIp;
        $data['dest_ip'] = $destIp;
        $data['type'] = $type;
        $data['trend'] = $trend;

        return $data;
    }
}